<template>
	<view class="u-wrap" :class="['qn-page-' + theme]">
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view class="u-tab-ul" v-for="(item, index) in cate_list" :key="index">
					<view class="u-tab-title">
						-
						<text class="u-tab-title-text">{{ item.title }}</text>
						-
					</view>
					<view class="u-tab-item" :class="[categoryId == item.id ? 'u-tab-item-active' : '']" :data-current="item.title" @tap.stop="swichMenu(item)">
						<text class="u-line-1">全部</text>
					</view>
					<block v-if="item.children && item.children.length > 0">
						<view
							v-for="(itemC, indexC) in item.children"
							:key="indexC"
							class="u-tab-item"
							:class="[categoryId == itemC.id ? 'u-tab-item-active' : '']"
							:data-current="itemC.title"
							@tap.stop="swichMenu(itemC)"
						>
							<text class="u-line-1">{{ itemC.title }}</text>
						</view>
					</block>
				</view>
			</scroll-view>
			<view class="right-box">
				<view class="u-search-box" @click="goPage('/pages/search/index')">
					<view class="u-search-inner">
						<u-icon name="search" :color="primaryColor" :size="28"></u-icon>
						<text class="u-search-text">请输入关键词</text>
					</view>
				</view>
				<scroll-view scroll-y class="right-box-scroll">
					<view class="page-view">
						<view class="cate-poster" v-if="adImage"><u-image width="100%" height="194rpx" :src="adImage"></u-image></view>
						<view class="item-title">
							<text>{{ current }}</text>
						</view>
						<view class="goods-ul">
							<Aempty text="没有商品哦～～" left="80rpx" src="../../static/empty/data.png" v-if="!goods_list.length"></Aempty>
							<view v-for="(item, index) in goods_list" :key="index" class="goods-li" @click="goPage('/pages/goods/GoodsDetail?id=' + item.id)">
								<view class="goods-img"><u-image width="100%" height="275rpx" :src="item.images[0]"></u-image></view>
								<view class="goods-info">
									<view class="goods-name ellipsis">{{ item.title }}</view>
									<view class="goods-desc ellipsis">{{ item.describe }}</view>
									<view class="goods-price primary-color" v-if="isShowPrice">¥{{ item.minSalePrice }}</view>
									<view class="goods-price primary-color" v-else>请登录</view>
								</view>
							</view>
						</view>
						<u-loadmore v-if="goods_list.length" :status="load_status" />
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			load_status: 'loadmore',
			page: 1,
			pageSize: 10,
			pageTotal: 0,
			t_current: 1, // 底部Tabbar，不要动
			adImage: '', //一级分类大图
			goods_list: [], // 商品列表
			cate_list: [], // 分类列表
			scrollTop: 0, //tab标题的滚动条位置
			current: '', // 预设当前项的值
			categoryId: '', // 预设当前项的值的id
			menuHeight: 0, // 左边菜单的高度
			menuItemHeight: 0 // 左边菜单item的高度
		};
	},
	computed: {
		isShowPrice() {
			const isShowPrice = this.$store.state.basicSet.goodsPrice || 5;
			if (this.$store.state.hasLogin) {
				return true;
			} else {
				return parseInt(isShowPrice) === 5;
			}
		}
	},
	created() {
		this.getAllCate();
	},
	methods: {
		getAllCate() {
			this.$u.api.getAllCate().then(({ data }) => {
				this.cate_list = data;
				if (data.length) {
					this.current = data[0].title;
					this.adImage = data[0].adImage;
					this.categoryId = data[0].id;
					this.searchGoods();
				}
			});
		},
		// 获取当前分类下的商品
		searchGoods() {
			this.$u.api
				.searchGoods({
					page: this.page,
					pageSize: this.pageSize,
					keyword: '',
					categoryId: this.categoryId,
					enableStatus: 5
				})
				.then(res => {
					if (this.page === 1) {
						this.goods_list = res.data;
					} else {
						this.goods_list = this.goods_list.concat(res.data);
					}
					this.pageTotal = res.pageTotal;
					this.load_status = this.$utils.loadStatus(this.goods_list, res.pageTotal);
				});
		},
		// 点击左边的栏目切换
		async swichMenu(row) {
			if (row.id == this.categoryId) return;
			this.current = row.title;
			this.categoryId = row.id;
			if (!row.pid) {
				this.adImage = row.adImage;
			} else {
				const obj = this.cate_list.find(item => item.id === row.pid);
				this.adImage = obj.adImage;
			}
			this.page = 1;
			this.searchGoods();
		}
	}
};
</script>

<style lang="scss" scoped>
.u-wrap {
	// height: calc(100vh);
	/* #ifdef H5 */
	// height: calc(100vh - var(--window-top));
	/* #endif */
	display: flex;
	flex-direction: column;
}

.u-search-box {
	padding: 18rpx 30rpx;
	.u-search-inner {
		background-color: #ffffff;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 20rpx;

		.u-search-text {
			font-size: 24rpx;
			color: $u-tips-color;
			margin-left: 20rpx;
		}
	}
}

.u-menu-wrap {
	flex: 1;
	display: flex;
	overflow: hidden;
}

.u-tab-view {
	width: 200rpx;
	height: calc(100vh - 100rpx);
	background-color: #ffffff;
	z-index: 1;
}
.u-tab-title {
	height: 80rpx;
	background: #ffffff;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 22rpx;
	color: #a9a9a9;
	font-weight: 400;
	line-height: 1;
	.u-tab-title-text {
		color: #666666;
		padding: 0 10upx;
	}
}
.u-tab-item {
	height: 80rpx;
	background: #ffffff;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 28rpx;
	color: #000000;
	font-weight: 400;
	line-height: 1;
}

.u-tab-item-active {
	position: relative;
	// color: #000;
	// font-size: 30rpx;
	// font-weight: 600;
	background: #fff;
}

.u-tab-item-active::before {
	content: '';
	position: absolute;
	border-left: 6rpx solid #f63434;
	height: 30rpx;
	left: 0;
	top: 50%;
	transform: translateY(-50%);
}

.right-box {
	width: 650rpx;
	background-color: rgb(250, 250, 250);
	.right-box-scroll {
		height: calc(100vh - 190rpx);
	}
}

.page-view {
	padding: 0 22upx 22rpx;
	.cate-poster {
		border-radius: 12upx;
		overflow: hidden;
	}
}

.item-title {
	font-size: 28rpx;
	color: $u-main-color;
	line-height: 130upx;
	text-align: center;
}
.goods-ul {
	display: flex;
	flex-wrap: wrap;
	background-color: #ffffff;
	border-radius: 12upx;
	overflow: hidden;
	margin-bottom: 20px;

	.goods-li {
		width: 50%;
		border-bottom: 1px solid #f7f8fa;
		.goods-info {
			padding: 20upx 10upx;
			text-align: center;
			.goods-img {
				padding-bottom: 10upx;
			}
			.goods-name {
				font-size: 24upx;
				color: #232323;
			}
			.goods-desc {
				margin: 10upx 0;
				font-size: 20upx;
				color: #525252;
				-webkit-line-clamp: 1;
			}
			.goods-price {
				color: $price-color;
			}
		}
	}
	.goods-li:nth-child(odd) {
		border-right: 1px solid #f7f8fa;
	}
}
</style>
